import axios from 'axios'
import qs from 'qs'
import { Message,Loading } from 'element-ui';
import router from '../router/index'

// 默认配置
axios.defaults.withCredentials = false
// axios.defaults.timeout = 50000
axios.defaults.headers = { 'X-Requested-With': 'XMLHttpRequest' }
// axios.defaults.baseURL = '/api'//process.env.API_ROOT


// 请求拦截器
axios.interceptors.request.use(
    config => {
        if (config.method === 'get') {
        }
        Loading.service({
            lock: true,
            text: '正在加载',
            background: 'rgba(0, 0, 0, 0)'

        });
        return config
    },
    err => {
        Message('请求错误')
        return Promise.reject(err)
    }
)
// 响应拦截器
axios.interceptors.response.use(
    response => {
        // 请求成功的时候更新token
        const token = response.headers["authorization"];  // 需要服务器设置 'Access-Control-Expose-Headers' : 'Authorization',
        if (token) {
            localStorage.setItem('token', token);
        }
        Loading.service({
            lock: true,
            text: '正在加载',
            background: 'rgba(0, 0, 0, 0)'
        }).close();
        return response
    },
    error => {
        if (error.response && error.response.status === 401) {
            router.push({
                path: '/login',
                query: {
                    url: encodeURIComponent(location.hash.substr(1))
                }
            })
        } else {
            Message.error({ showClose: true, message: '请重新进入！' + error, type: 'error' ,duration: 2000,offset: 20})
            Loading.service({
                lock: true,
                text: '正在加载',
                background: 'rgba(0, 0, 0, 0)'
            }).close();
            console.log(error.response)
        }
        
        return Promise.reject(error)
    }
)
// 错误处理
function checkStatus(response) {
    return new Promise((resolve, reject) => {
        if (response.status === 200) {
            resolve(response.data)
        } else {
            reject(response.data)
        }
    })
}

export default {
    get(baseurl,url, params) {
        return axios({
            baseURL: baseurl,
            method: 'get',
            url,
            params,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'Authorization': `${localStorage.getItem('token')}`
            }
        }).then(checkStatus)
    },
    post(baseurl,url, data) {
        return axios({
            baseURL: baseurl,
            method: 'post',
            url,
            data: qs.stringify(data),
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                'Authorization': `${localStorage.getItem('token')}`
            }
        }).then(checkStatus)
    },
    put(url, data) {
        return axios({
            method: 'put',
            url,
            data: qs.stringify(data)
        }).then(checkStatus)
    },
    putParams(url, params) {
        return axios({
            method: 'put',
            url,
            params
        }).then(checkStatus)
    },
    putForm(url, data) {
        return axios({
            method: 'put',
            url,
            data: qs.stringify(data),
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        })
    },
    delete(url, params) {
        return axios({
            method: 'delete',
            url,
            params
        }).then(checkStatus)
    }

}